﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class Tab_NewsDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="NewID">标识列ID</param>
		public bool Exists(int NewID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Tab_News] where NewID=@newid ");
			htSyn.Add("@newid", NewID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Tab_News> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [NewID],[NewKey],[NewDescription],[NewAddDate],[NewEditDate],[NewTitle],[NewName],[NewClassID],[NewInfo] FROM [Tab_News]");
			 return getTab_NewsBySql(strSql.ToString());
		}
		#endregion

        #region 根据新闻类别查询新闻列表
        /// <summary>
        /// 根据新闻类别查询新闻列表
        /// </summary>
        /// <param name="newClassID">新闻类别ID</param>
        public IList<Tab_News> GetModelByClass(int newClassID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("Select [NewID],[NewKey],[NewDescription],[NewAddDate],[NewEditDate],[NewTitle],[NewName],[NewClassID],[NewInfo] FROM [Tab_News] where [NewClassID]=@NewClassID");
            htSyn.Add("@NewClassID", newClassID);
            return getTab_NewsBySql(strSql.ToString(),htSyn);
        }
        #endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="NewID">标识列ID</param>
		/// <returns></returns>
		public Tab_News GetModel(int newid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [NewID],[NewKey],[NewDescription],[NewAddDate],[NewEditDate],[NewTitle],[NewName],[NewClassID],[NewInfo] FROM [Tab_News]");
			strSql.Append(" where NewID=@newid ");
			htSyn.Add("@newid", newid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Tab_News tab_newsModel = new Tab_News();
			if (dt.Rows.Count > 0)
			{
				tab_newsModel.newKey=dt.Rows[0]["NewKey"].ToString();
				tab_newsModel.newDescription=dt.Rows[0]["NewDescription"].ToString();
				tab_newsModel.newAddDate=dt.Rows[0]["NewAddDate"].ToString();
				tab_newsModel.newEditDate=dt.Rows[0]["NewEditDate"].ToString();
				tab_newsModel.newTitle=dt.Rows[0]["NewTitle"].ToString();
				tab_newsModel.newName=dt.Rows[0]["NewName"].ToString();
				tab_newsModel.newClassID=Convert.ToInt32(dt.Rows[0]["NewClassID"]);
				tab_newsModel.newInfo=dt.Rows[0]["NewInfo"].ToString();
			}
			return tab_newsModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Tab_News">实体</param>
		/// <returns></returns>
		public bool Update(Tab_News tab_newsModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Tab_News] set ");
			strSql.Append("[NewKey]=@newkey,");
			strSql.Append("[NewDescription]=@newdescription,");
			strSql.Append("[NewAddDate]=@newadddate,");
			strSql.Append("[NewEditDate]=@neweditdate,");
			strSql.Append("[NewTitle]=@newtitle,");
			strSql.Append("[NewName]=@newname,");
			strSql.Append("[NewClassID]=@newclassid,");
			strSql.Append("[NewInfo]=@newinfo");

			strSql.Append(" where NewID=@newid ");
			htSyn.Add("@newid", tab_newsModel.newID);
			htSyn.Add("@newkey", tab_newsModel.newKey);
			htSyn.Add("@newdescription", tab_newsModel.newDescription);
			htSyn.Add("@newadddate", tab_newsModel.newAddDate);
			htSyn.Add("@neweditdate", tab_newsModel.newEditDate);
			htSyn.Add("@newtitle", tab_newsModel.newTitle);
			htSyn.Add("@newname", tab_newsModel.newName);
			htSyn.Add("@newclassid", tab_newsModel.newClassID);
			htSyn.Add("@newinfo", tab_newsModel.newInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="NewID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int NewID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Tab_News] where NewID=@newid");
			htSyn.Add("newid", NewID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Tab_News">实体</param>
		/// <returns></returns>
		public bool Insert(Tab_News tab_newsModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [Tab_News]([NewKey],[NewDescription],[NewAddDate],[NewEditDate],[NewTitle],[NewName],[NewClassID],[NewInfo]) values (@newkey,@newdescription,@newadddate,@neweditdate,@newtitle,@newname,@newclassid,@newinfo)");
			htSyn.Add("@newkey", tab_newsModel.newKey);
			htSyn.Add("@newdescription", tab_newsModel.newDescription);
			htSyn.Add("@newadddate", tab_newsModel.newAddDate);
			htSyn.Add("@neweditdate", tab_newsModel.newEditDate);
			htSyn.Add("@newtitle", tab_newsModel.newTitle);
			htSyn.Add("@newname", tab_newsModel.newName);
			htSyn.Add("@newclassid", tab_newsModel.newClassID);
			htSyn.Add("@newinfo", tab_newsModel.newInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Tab_News> getTab_NewsBySql(string strSql)
		{
			IList<Tab_News> list = new List<Tab_News>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Tab_News tab_newsModel = new Tab_News();
					tab_newsModel.newID= Convert.ToInt32(dr["NewID"]);
					tab_newsModel.newKey=dr["NewKey"].ToString();
					tab_newsModel.newDescription=dr["NewDescription"].ToString();
					tab_newsModel.newAddDate=dr["NewAddDate"].ToString();
					tab_newsModel.newEditDate=dr["NewEditDate"].ToString();
					tab_newsModel.newTitle=dr["NewTitle"].ToString();
					tab_newsModel.newName=dr["NewName"].ToString();
					tab_newsModel.newClassID=Convert.ToInt32(dr["NewClassID"]);
					tab_newsModel.newInfo=dr["NewInfo"].ToString();
					list.Add(tab_newsModel);
				}
			}
			return list;
		}
		#endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="ht">参数</param>
        public IList<Tab_News> getTab_NewsBySql(string strSql,Hashtable ht)
        {
            IList<Tab_News> list = new List<Tab_News>();
            DataTable dt = db.GetDataTable(strSql,ht);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Tab_News tab_newsModel = new Tab_News();
                    tab_newsModel.newID = Convert.ToInt32(dr["NewID"]);
                    tab_newsModel.newKey = dr["NewKey"].ToString();
                    tab_newsModel.newDescription = dr["NewDescription"].ToString();
                    tab_newsModel.newAddDate = dr["NewAddDate"].ToString();
                    tab_newsModel.newEditDate = dr["NewEditDate"].ToString();
                    tab_newsModel.newTitle = dr["NewTitle"].ToString();
                    tab_newsModel.newName = dr["NewName"].ToString();
                    tab_newsModel.newClassID =Convert.ToInt32(dr["NewClassID"]);
                    tab_newsModel.newInfo = dr["NewInfo"].ToString();
                    list.Add(tab_newsModel);
                }
            }
            return list;
        }
        #endregion

        #region 新闻分页
        /// <summary>
        /// 新闻分页
        /// </summary>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">页数</param>
        /// <returns></returns>
        public DataTable pageForNews(int pageIndex,int pageSize) 
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "[NewID],[NewKey],[NewDescription],[NewAddDate],[NewEditDate],[NewTitle],[NewName],[NewClassID],[NewInfo]", "[Tab_News]","","[NewID]", false);
        }
        #endregion

        #region 新闻分页
        /// <summary>
        /// 新闻分页
        /// </summary>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">页数</param>
        /// <param name="newClassID">新闻类别ID</param>
        /// <returns></returns>
        public DataTable pageForNews(int pageIndex, int pageSize,int newClassID)
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "[NewID],[NewKey],[NewDescription],[NewAddDate],[NewEditDate],[NewTitle],[NewName],[NewClassID],[NewInfo]", "[Tab_News]", "and [NewClassID]="+newClassID, "[NewID]", false);
        }
        #endregion

        #region 获得新闻数
        /// <summary>
        /// 获得新闻数
        /// </summary>
        /// <returns></returns>
        public int getRecordCount() 
        {
            object objRecordCount = null;
            int i = 0;
            objRecordCount = db.ExecuteScalar("select count(1) from [Tab_News]");
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion

        #region 获得新闻数
        /// <summary>
        /// 获得新闻数
        /// </summary>
        /// <param name="newClassID">新闻类别ID</param>
        /// <returns></returns>
        public int getRecordCount(int newClassID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            object objRecordCount = null;
            int i = 0;
            strSql.Append("select count(1) from [Tab_News] Where [NewClassID]=@NewClassID");
            htSyn.Add("@NewClassID",newClassID);
            objRecordCount = db.ExecuteScalar(strSql.ToString(), htSyn);
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion
    }
}
